package cn.ffcs.bpmpvc.web;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.echoice.paper.PageBean;
import org.echoice.paper.PageNavi;
import cn.ffcs.base.web.BaseAction;
import cn.ffcs.bpm.model.ProcTranRecord;
import cn.ffcs.bpm.model.ProcessConfig;
import cn.ffcs.bpm.model.Report;
import cn.ffcs.bpm.model.TransactProcessStatus;
import cn.ffcs.bpmpvc.dao.FlowPvcDao;
import cn.ffcs.bpmpvc.services.AppBusinessReqService;
import cn.ffcs.bpmpvc.services.AppDataTranferService;
import cn.ffcs.bpmpvc.svc.ProcessPvcService;
import cn.ffcs.bpmpvc.svc.TaskService;
import cn.ffcs.bpmpvc.web.forms.FlowReqReportForm;
import cn.ffcs.businessrequest.model.BusinessRequest;
import cn.ffcs.security.model.User;
import cn.ffcs.util.context.ApplicationContextUtil;

public class FlowWeekReportAction<D extends TransactProcessStatus> extends
		BaseAction<D> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static String week = "01";
	private static String month = "02";
	private ProcessPvcService processPvcService;
	private TaskService taskService;
	private AppBusinessReqService appBusinessReqService;
	private FlowReqReportForm flowReqReportForm;
	private List<ProcessConfig> processList;
	private AppDataTranferService appDataTranferService;

	/**
	 * 查询周报月报配置
	 * @return
	 */
	public String findNeedDeploy() {
		HttpServletRequest request = ApplicationContextUtil
				.getHttpServletRequest();
		int pageSize = PageNavUtil.getPageSize(request);
		this.findNeed(pageSize,null,null);
		return "needDeploy";
	}
	/**
	 * 跳转至配置页
	 * @return
	 */
	public String toDeploy(){
		HttpServletRequest request = ApplicationContextUtil.getHttpServletRequest();
		String reqId=request.getParameter("reqId");
		BusinessRequest businessRequest=getAppBusinessReqService().getAppBusinessReqDao().getBusinessRequestById(Integer.parseInt(reqId));
		request.setAttribute("businessRequest", businessRequest);
		return "weekDeploy";
	}
	/**
	 * 保存配置
	 * @return
	 */
	public String deploy(){
		HttpServletRequest request = ApplicationContextUtil.getHttpServletRequest();
		String reqId=request.getParameter("reqId");
		BusinessRequest businessRequest=getAppBusinessReqService().getAppBusinessReqDao().getBusinessRequestById(Integer.parseInt(reqId));
		String weekState=request.getParameter("weekState");
		String monthState=request.getParameter("monthState");
		if(weekState!=null&&!"".equals(weekState)){
			businessRequest.setWeekState(weekState);
		}
		if(monthState!=null&&!"".equals(monthState)){
			businessRequest.setMonthState(monthState);
		}
		getAppBusinessReqService().getAppBusinessReqDao().save(businessRequest);
		return findNeedDeploy();
	}
	/**
	 * 周报月报报表显示
	 * 
	 * @return
	 */
	public String findWeekReport() {
		HttpServletRequest request = ApplicationContextUtil
				.getHttpServletRequest();
		int pageSize = PageNavUtil.getPageSize(request);
		return this.findWeekReport(pageSize);
	}

	/**
	 * 需要填写的周报
	 */
	public String findNeedWeek() {
		HttpServletRequest request = ApplicationContextUtil
				.getHttpServletRequest();
		int pageSize = PageNavUtil.getPageSize(request);
		User user = ApplicationContextUtil.getUserContextObject().getCurrUser();
		this.findNeed(pageSize,user,week);
		return "needWeek";
	}

	/**
	 * 需要填写的月报
	 */
	public String findNeedMonth() {
		HttpServletRequest request = ApplicationContextUtil
				.getHttpServletRequest();
		int pageSize = PageNavUtil.getPageSize(request);
		User user = ApplicationContextUtil.getUserContextObject().getCurrUser();
		this.findNeed(pageSize,user,month);
		return "needMonth";
	}

	@SuppressWarnings("unchecked")
	public String findNeed(int pageSize,User user,String type) {
		HttpServletRequest request = ApplicationContextUtil.getHttpServletRequest();
		FlowPvcDao flowPvcDao = getProcessPvcService().getFlowPvcDao();
		PageBean pageBean = getAppBusinessReqService().getAppBusinessReqDao().queryPageNeedWeek(flowReqReportForm, user,type,PageNavUtil.getPageNo(request), pageSize);
		List<BusinessRequest> bsReqList = pageBean.getDataList();
		if (bsReqList != null && bsReqList.size() > 0) {
			List<ProcTranRecord> procRecordList = null;
			int i = 0;
			for (BusinessRequest businessRequest : bsReqList) {
				// 取得流程运行中的任务记录
				procRecordList = flowPvcDao.findRunTaskList(businessRequest
						.getProcinstId());
				if (procRecordList != null && procRecordList.size() > 0) {
					for (ProcTranRecord procTranRecord : procRecordList) {
						if ("active".equals(procTranRecord.getState())) {
							businessRequest.setCurrStatus(procTranRecord.getProcessStep().getName());
							businessRequest.setCurrUser(procTranRecord.getTransactor().getName());
						}
					}
				} else {
					businessRequest.setCurrUser("");
					businessRequest.setCurrStatus("结束");
				}
				i++;
			}
		}
		request.setAttribute("reportList", bsReqList);
		String pageNaviStr = PageNavi.getPageNaviText(request, pageBean);
		request.setAttribute("pageNaviStr", pageNaviStr);

		return null;
	}

	/**
	 * 跳转
	 * 
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public String toAdd() {
		HttpServletRequest request = ApplicationContextUtil.getHttpServletRequest();
		String reqId = request.getParameter("reqId");
		String type = request.getParameter("type");
		Report report =null;
		if(reqId==null||"".equals(reqId)){
			String taskId=request.getParameter("flowTaskId");
			report = getAppBusinessReqService().getAppBusinessReqDao().getReportById(Integer.parseInt(taskId));
			reqId=report.getReqId();
			request.setAttribute("msg", "true");
		}else{
			report = getAppBusinessReqService().getAppBusinessReqDao()
					.getLastReport(reqId, type);
		}
		int dwNum=0;
		if("01".equals(type)){
			dwNum =getAppBusinessReqService().getAppBusinessReqDao().weekNum();
		}else{
			Date date=new Date();
			dwNum=date.getMonth()+1;
		}
		report.setDwNum(String.valueOf(dwNum));
		request.setAttribute("reqId", reqId);
		request.setAttribute("type", type);
		request.setAttribute("report", report);
		return "addWeek";
	}

	/**
	 * 查看
	 * 
	 * @return
	 */
	public String infoReprotList() {
		HttpServletRequest request = ApplicationContextUtil
				.getHttpServletRequest();
		String reqId = request.getParameter("reqId");
		String type = request.getParameter("type");
		request.setAttribute("reqId", reqId);
		request.setAttribute("type", type);
		BusinessRequest businessRequest = getAppBusinessReqService()
				.getAppBusinessReqDao().getBusinessRequestById(
						Integer.parseInt(reqId));
		List<Report> reprotList = getAppBusinessReqService()
				.getAppBusinessReqDao().reportList(reqId, type);
		request.setAttribute("reprotList", reprotList);
		request.setAttribute("businessRequest", businessRequest);
		return "infoReprotList";
	}

	/**
	 * 添加或修改
	 * 
	 * @return
	 */
	public String addOrUpdate() {
		HttpServletRequest request = ApplicationContextUtil
				.getHttpServletRequest();
		User user = ApplicationContextUtil.getUserContextObject().getCurrUser();
		String type = request.getParameter("type");
		String msg = request.getParameter("msg");
		
		String id = request.getParameter("id");
		Report report = new Report();
		if (id != null && !"".equals(id)) {
			report = getAppBusinessReqService().getAppBusinessReqDao()
					.getReportById(Integer.parseInt(id));
		}
		report.setOperateTime(new Date());
		report.setReqId(request.getParameter("reqId"));
		report.setOperateInfo(request.getParameter("operateInfo").replaceAll(
				"<br/>", ""));
		report.setHelpItem(request.getParameter("helpItem").replaceAll("<br/>",
				""));
		report.setNextWork(request.getParameter("nextWork").replaceAll("<br/>",
				""));
		report.setType(type);
		report.setPersonId(user.getPersonID());
		if(report.getReceiveTime()==null){
			report.setReceiveTime(new Date());
		}
		report.setDwNum(request.getParameter("dwNum"));
		getAppBusinessReqService().getAppBusinessReqDao().save(report);
		// getProcessPvcService().getFlowPvcDao().save(report);
		if("true".equals(msg)){
			this.returnMsg="已成功填写报表！";
			return "jumpMsg";
		}
		if (week.equals(type)) {
			return findNeedWeek();
		} else {
			return findNeedMonth();
		} 
	}
	public static void main(String[] args) {
		System.out.println(Calendar.getInstance().get(Calendar.WEEK_OF_YEAR));
		 Calendar c = Calendar.getInstance(Locale.CHINA);
	        String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));
	        if (week.length() == 1) {
	            week = "0" + week;
	        }
	        String year = Integer.toString(c.get(Calendar.YEAR));
	        System.out.println(year + week); 
	}
	/**
	 * 周报月报
	 * 
	 * @param pageSize
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String findWeekReport(int pageSize) {
		HttpServletRequest request = ApplicationContextUtil
				.getHttpServletRequest();
		FlowPvcDao flowPvcDao = getProcessPvcService().getFlowPvcDao();
		processList = flowPvcDao.findAllProcessList();
		List<ProcessConfig> listProcessConfig = new ArrayList<ProcessConfig>();
		ProcessConfig processConfig = new ProcessConfig();
		processConfig.setId(null);
		processConfig.setName("请选择流程");
		listProcessConfig.add(processConfig);
		for (int i = 0; i < processList.size(); i++) {
			processConfig = processList.get(i);
			if ("1".equals(processConfig.getIsDefault())) {
				listProcessConfig.add(processConfig);
			}
		}
		processList = listProcessConfig;
		List<String> typeList = new ArrayList<String>();
		List types = this.getAppBusinessReqService().getAppBusinessReqDao()
				.getDaiTypes("");
		for (int i = 0; i < types.size(); i++) {
			Map<String, String> m = (Map<String, String>) types.get(i);
			typeList.add(m.get("dai_type"));
		}
		if (flowReqReportForm != null) {
			PageBean pageBean = getAppBusinessReqService()
					.getAppBusinessReqDao().queryPageWeekReportList(
							flowReqReportForm, PageNavUtil.getPageNo(request),
							pageSize);
			List<BusinessRequest> bsReqList = pageBean.getDataList();
			if (bsReqList != null && bsReqList.size() > 0) {
				List<ProcTranRecord> procRecordList = null;
				String taskIds = "";
				int i = 0;
				for (BusinessRequest businessRequest : bsReqList) {
					// 取得流程运行中的任务记录
					procRecordList = flowPvcDao.findRunTaskList(businessRequest
							.getProcinstId());
					taskIds = "";
					if (procRecordList != null && procRecordList.size() > 0) {
						for (ProcTranRecord procTranRecord : procRecordList) {
							if ("active".equals(procTranRecord.getState())) {
								businessRequest.setCurrStatus(procTranRecord
										.getProcessStep().getName());
								businessRequest.setCurrUser(procTranRecord
										.getTransactor().getName());
							}
							taskIds += "'" + procTranRecord.getId() + "'" + ",";
						}
					} else {
						businessRequest.setCurrUser("");
						businessRequest.setCurrStatus("结束");
					}
					i++;
				}
			}
			request.setAttribute("reportList", bsReqList);
			String pageNaviStr = PageNavi.getPageNaviText(request, pageBean);
			request.setAttribute("pageNaviStr", pageNaviStr);
		}
		request.setAttribute("types", typeList);
		return SUCCESS;
	}

	public ProcessPvcService getProcessPvcService() {
		return processPvcService;
	}

	public void setProcessPvcService(ProcessPvcService processPvcService) {
		this.processPvcService = processPvcService;
	}

	public TaskService getTaskService() {
		return taskService;
	}

	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}

	public AppBusinessReqService getAppBusinessReqService() {
		return appBusinessReqService;
	}

	public void setAppBusinessReqService(
			AppBusinessReqService appBusinessReqService) {
		this.appBusinessReqService = appBusinessReqService;
	}

	public FlowReqReportForm getFlowReqReportForm() {
		return flowReqReportForm;
	}

	public void setFlowReqReportForm(FlowReqReportForm flowReqReportForm) {
		this.flowReqReportForm = flowReqReportForm;
	}

	public List<ProcessConfig> getProcessList() {
		return processList;
	}

	public void setProcessList(List<ProcessConfig> processList) {
		this.processList = processList;
	}

	public AppDataTranferService getAppDataTranferService() {
		return appDataTranferService;
	}

	public void setAppDataTranferService(
			AppDataTranferService appDataTranferService) {
		this.appDataTranferService = appDataTranferService;
	}

}
